package apis

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/api"
	"github.com/gin-gonic/gin"

	"ssdevice/internal/device/models"
	"ssdevice/internal/device/models/query"
)

type Operate struct {
	api.Api
}

// 查询操作记录
func (e Operate) FindOperateList(c *gin.Context) {
	// 请求参数
	req := query.SysOperateQuery{}
	// 模型
	m := models.SysOperate{}
	// 上下文
	err := e.MakeContext(c).
		Bind(&req).
		MakeModel(&m.Model).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}

	// 设置当前用户
	req.LoadUser(c)
	req.Username = req.UserInfo.UserName

	// 执行查询
	var result []*models.SysOperate
	var total int64
	if err, result, total = m.FindOperateList(&req); err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}

	// 返回分页结果
	e.Page(total, req.PageSize, req.PageIndex, result)
}
